//: Playground - noun: a place where people can play

import UIKit

// swift字典的键值可以是Int/Double等类型

// 初始化字典

// (1) 不可变
let dict0 = ["key0" : "value", "key1" : 23] as [String : Any] // 需要转换,值的类型不确定
let dict1 = ["key0" : "value0", "key1" : "value1"] // 不需要转换,值的类型确定
let dict2 : Dictionary<String, Any> = ["key0" : "value", "key1" : 23] // 前面指定了,后面也不需要转换
let dict3 :[String : Any] = ["key0" : "value", "key1" : 23] // 前面指定了,后面也不需要转换

// (2) 可变
var dictM0 = Dictionary<String, Any>()

// 增
dictM0["name"] = "AY"
dictM0["age"] = 23
dictM0

// 删
dictM0.removeValue(forKey: "name")

// 改
dictM0["age"] = 25
dictM0

// 查
let age = dictM0["age"]

//var dictM1 = [String : Any]()
//dictM1["name"] = "Alex"
//dictM1

// 遍历字典
// (1) 遍历所有的键
for key in dict0.keys {
    print(key)
}

// (2) 遍历所有的value
for value in dict0.values {
    print(value)
}

// (3) 遍历所有的键值
for (key, value) in dict0 {
    print(key, value)
}

// 合并字典
let dict5 : [String : Any] = ["name" : "Alex", "age" : 18, "height" : 180.0]
var dict6 : [String : Any] = ["phoneNumber" : "123", "alias" : "AY"]
//let result = dict5 + dict6  // 不能这样进行合并操作

// 可以这样合并
for (key, value) in dict5 {
    dict6[key] = value
}
dict6




